﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace CloudClip.ExtensionMethod
{
    public static class StringExtension
    {
        public static bool IsNullOrEmpty(this string value)
        {
            if (value != null)
            {
                value = value.Trim();
            }
            return String.IsNullOrEmpty(value);
        }

        public static string RemoveLastChar(this string value)
        {
            if (value.IsNullOrEmpty())
            {
                return string.Empty;
            }
            return value.Remove(value.Length - 1, 1);
        }

        public static string RemoveLastLN(this string value)
        {
            if (value.IsNullOrEmpty())
            {
                return string.Empty;
            }
            return value.TrimEnd(Environment.NewLine.ToCharArray());
        }

        public static string RemoveLastBR(this string value)
        {
            string sBR = "<br/>";

            if (value.IsNullOrEmpty())
            {
                return string.Empty;
            }
            return value.TrimEnd(sBR.ToCharArray());
        }

        public static string ReplaceNewLine(this string value, string sToken)
        {
            //有時會是\n,先置換 邏輯=> 找不到\r\n 就置換\n
            if (value.IndexOf(Environment.NewLine) == -1)
            {
                value = value.Replace("\n", Environment.NewLine);
            }
            return value.Replace(Environment.NewLine, sToken);
        }

        /// <summary>
        /// 傳回原string，若原string為null，則傳回空字串
        /// </summary>
        /// <param name="sValue"></param>
        /// <returns></returns>
        public static string ValueNoneNull(this string sValue)
        {
            if (sValue == null)
                return "";

            return sValue;
        }


        public static int ToInt(this string sValue)
        {
            return Convert.ToInt32(sValue);
        }



        public static string SubstringEx(this string sValue, int iStartIndex, int iLength)
        {
            if (sValue.Length > iLength)
            {
                return sValue.Substring(iStartIndex, iLength);//只取Length的長度

            }
            else
            {
                return sValue.Substring(iStartIndex);//全部
            }
        }

        /// <summary>
        /// 讓中英文或符號取出來的字串長度在畫面上幾乎一樣
        /// </summary>
        /// <param name="sValue"></param>
        /// <param name="iLength"></param>
        /// <returns></returns>
        public static string SubstringEx2(this string sValue, int iLength)
        {
            int len = sValue.Length;
            if (len < iLength) return sValue;
            int clen = 0;
            int cwidth = 0;
            while (clen < len && cwidth < iLength)
            {
                if ((int)sValue[clen] > 128) cwidth++;
                clen++;
                cwidth++;
            }
            return sValue.Substring(0, clen); 
        }


        /// <summary>
        /// 把%的字元換成.*，用Regex找出符合的字元
        /// </summary>
        /// <param name="value"></param>
        /// <param name="sFilter"></param>
        /// <returns></returns>
        public static bool Like(this string value, string sFilter)
        {
            if (value.IsNullOrEmpty())
            {
                return false;
            }

            //將特列字元逸出
            sFilter = Regex.Escape(sFilter).ToLower();
            value = value.ToLower();

            if (!sFilter.Contains("%"))
            {
                sFilter = "%" + sFilter + "%";
            }

            sFilter = "^" + sFilter.Replace("%", ".*") + "$";

            if (Regex.IsMatch(value, sFilter))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static string XMLEncode(this string sValue)
        {
            //處理XML中的保留字
            sValue = sValue.Replace("\"", "&quot;");
            sValue = sValue.Replace("\'", "&apos;");
            sValue = sValue.Replace("&", "&amp;");
            sValue = sValue.Replace("<", "&lt;");
            sValue = sValue.Replace(">", "&gt;");

            return sValue;
        }

        public static string XMLDecode(this string sValue)
        {
            sValue = sValue.Replace("&quot;", "\"");
            sValue = sValue.Replace("&apos;", "\'");
            sValue = sValue.Replace("&amp;", "&");
            sValue = sValue.Replace("&lt;", "<");
            sValue = sValue.Replace("&gt;", ">");

            return sValue;
        }

        public static string[] Split(this string sValue, string sStringToken)
        {
            string[] split = sValue.Split(new string[] { sStringToken }, StringSplitOptions.RemoveEmptyEntries);//QQ
            return split;
        }

        public static string[] SplitWithEnvironmentNewLine(this string sValue)
        {
            string[] split = sValue.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);//QQ

            return split;
        }

        public static string[] SplitWithTab(this string sValue)
        {
            string[] split = sValue.Split(new char[] { '\t' });

            return split;
        }

        public static string[] SplitWithCommon(this string sValue)
        {
            string[] split = sValue.Split(new char[] { ',' });

            return split;
        }

        public static string[] SplitWithEqual(this string sValue)
        {
            string[] split = sValue.Split(new char[] { '=' });

            return split;
        }

        /// <summary>
        /// 檢查是否為符合規則的檔案，即檢查/\:*?"<>|
        /// </summary>
        /// <returns></returns>
        public static bool IsFileNameContainRefusedChar(this string sValue)
        {
            return Regex.IsMatch(sValue, "[/,:,*,?,\",<,>,|]") || sValue.Contains(@"\");
        }

        public static List<string> ToList(this string sValue, string sComma)
        {
            if (sValue.IsNullOrEmpty())
                return new List<string>();

            List<string> lstReturn = new List<string>();

            foreach (string str in sValue.Split(Convert.ToChar(sComma)))
            {
                if (str.IsNullOrEmpty() == false)
                    lstReturn.Add(str);
            }

            return lstReturn;
        }


        public static string ConcatWithCommon(this string sOrigin, string sAppend)
        {
            if (sOrigin.IsNullOrEmpty())
            {
                return sAppend;
            }
            else
            {
                return sOrigin + "," + sAppend;
            }
        }


        public static string ToFileFormatString(this string sValue)
        {
            string sReplaceTo = "_";

            if (sValue.IsNullOrEmpty() == false)
            {
                sValue = sValue.Replace(@"<", sReplaceTo);
                sValue = sValue.Replace(@">", sReplaceTo);
                sValue = sValue.Replace(@"/", sReplaceTo);
                sValue = sValue.Replace(@"\", sReplaceTo);
                sValue = sValue.Replace(@"|", sReplaceTo);
                sValue = sValue.Replace(@"?", sReplaceTo);
                sValue = sValue.Replace(@":", sReplaceTo);
                sValue = sValue.Replace(@"*", sReplaceTo);
            }

            return sValue;
        }

        public static bool NotIn(this string sValue, string[] arrNotIn)
        {
            return !arrNotIn.Contains(sValue);
        }

        public static bool In(this string sValue, string[] arrNotIn)
        {
            return arrNotIn.Contains(sValue);
        }
    }
}
